package com.chushouya.order.service.api.impl;

import com.general.framework.core.lang.Beans;
import com.general.framework.core.lang.Dates;
import com.chushouya.order.constants.DataScopeEnum;
import com.chushouya.order.dao.repository.StatClerkOrderDayRepository;
import com.chushouya.order.dto.api.clerk.ClerkOrderDataDTO;
import com.chushouya.order.dto.api.clerk.ClerkOrderDataQuery;
import com.chushouya.order.dto.api.clerk.ClerkOrderDayStatDTO;
import com.chushouya.order.dto.api.stat.ClerkOrderDayStatQuery;
import com.chushouya.order.service.api.ClerkDashboardApiService;
import com.chushouya.order.service.common.ClerkOrderDayStatTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class ClerkDashboardApiServiceImpl implements ClerkDashboardApiService {

    @Resource
    private StatClerkOrderDayRepository clerkOrderDayStatRepository;

    @Resource
    private ClerkOrderDayStatTaskService clerkOrderDayStatTaskService;

    @Override
    public ClerkOrderDataDTO orderTotalData(ClerkOrderDataQuery query) {
        // 默认同步一次今日数据
        clerkOrderDayStatTaskService.clerkOrderTodayStat(query.getClerkId(), Dates.getTimeNow());
        final DataScopeEnum dataScope = DataScopeEnum.get(query.getDataScope());
        return clerkOrderDayStatRepository.selectOrderCount(query.getClerkId(), dataScope.timeStart(), dataScope.timeEnd());
    }

    @Override
    public List<ClerkOrderDayStatDTO> orderDayData(ClerkOrderDataQuery query) {
        // 默认同步一次今日数据
        clerkOrderDayStatTaskService.clerkOrderTodayStat(query.getClerkId(), Dates.getTimeNow());
        ClerkOrderDayStatQuery queryData = new ClerkOrderDayStatQuery();
        queryData.setClerkId(query.getClerkId());
        queryData.setStatDateStart(Dates.getMonthFirstTime(query.getMonth()));
        queryData.setStatDateEnd(Dates.getMonthLastTime(query.getMonth()));
        return Beans.copyList(clerkOrderDayStatRepository.selectList(queryData), ClerkOrderDayStatDTO.class);
    }

}
